#include <iostream>
#include <cstdio>
#include <string.h>

// int main()
// {
//     FILE* fp = fopen("bite", "w+");
//     if(fp == nullptr)
//     {
//         perror("fopen fail!");
//         return -1;
//     }

//     char wbuffer[64];
//     memset(wbuffer, 0, sizeof(wbuffer));
//     snprintf(wbuffer, sizeof(wbuffer), "Linux so easy!");
//     int len = strlen(wbuffer) + 1;
//     fwrite(wbuffer, 1, len, fp);

//     fseek(fp, 0, SEEK_SET);
//     char rbuffer[1024];
//     memset(wbuffer, 0, sizeof(rbuffer));
//     fread(rbuffer, 1, len, fp);
//     std::cout << rbuffer << std::endl;

//     fseek(fp, 0, SEEK_END);
//     long file_size = ftell(fp);
//     std::cout << "file_size: " <<  file_size << std::endl;
//     rewind(fp);

//     fclose(fp);
//     return 0;
// }


/* 场景1 */
int main()
{
    FILE* fp = fopen("Test1", "w+");
    if(fp == nullptr)
    {
        perror("fopen fail!");
        return -1;
    }

    const char* msg = "Hello World!";
    // char msg[64];
    // snprintf(msg, sizeof(msg), "Hello World!");
    int len = strlen(msg) + 1;
    fwrite(msg, 1, len, fp);
    int pos = ftell(fp);
    
    // rewind(fp);
    fseek(fp, 0, SEEK_SET);
    char rbuffer[1024];
    fwrite(rbuffer, 1, len, fp);
    std::cout << rbuffer << std::endl;

    fclose(fp);
    return 0;
}